<?php

class Admin_Input extends Controller
{
	var $data=array();
	
	public function __construct()
	{
		parent::Controller();
        include("password_protect.php");
		$this->load->model('Admin_Input_Model');
		$this->load->model('Admin_Quotation_Model');
		
		$this->data['warning']="";
		$this->data['success']="";
		$this->data['city']="";
		$this->data['province_state']="";
		$this->data['country']="";
		$this->data['province_list']=$this->get_province_list();
		$this->data['your_number']="";
		$this->data['province_exists']="";
		$this->data['city_exists']="";
		$this->data['search_city_list']=array();
		$this->data['search_province_list']=array();
		$this->data['search']="";
		$this->data['search_province']="";
		$this->data['hidden_province_id']=-1;
		$this->data['search_city_id']=-1;
		
		$this->data['other_comment']="";
		$this->data['shipdest_comment']="";
		
		$this->data['quote_list']=null;	


		$this->data['email']="";
		$this->data['requester_name']="";
		$this->data['requesting_comp_name']="";
		$this->data['requesting_comp_name']="";
		$this->data['requester_phone']="";
		$this->data['designator_id']=-1;
		$this->data['designator_title']="";
		$this->data['city_list']="";

		$this->data['pickup_time']="";
		$this->data['pickup_time_period']="";
		$this->data['dock_closed_time']="";
		$this->data['dock_closed_time_period']="";
		
		$this->data['pickup_date']="";
		$this->data['pickup_contact_name']="";
		$this->data['pickup_address']="";
		$this->data['pickup_city']="";
		$this->data['pickup_province']="";
		$this->data['pickup_country']="";
		$this->data['pickup_p_code']="";
		$this->data['pickup_fax']="";
		$this->data['pickup_company_name']="";
		$this->data['pickup_phone_number']="";

		$this->data['destination_contact_name']="";
		$this->data['destination_address']="";
		$this->data['destination_p_code']="";
		$this->data['destination_city']="";
		$this->data['destination_province']="";
		$this->data['destination_country']="";
		$this->data['destination_fax']="";
		$this->data['destination_company_name']="";
		$this->data['destination_phone_number']="";
		
		$this->data['goods_description']="";
		$this->data['total_pieces']="";
		$this->data['total_weight']="";
		$this->data['hand_inst']="";
		$this->data['is_hazardous']=-1;

		$this->data['designator_id']=-1;
	}
	
	function index()
	{
		if($this->input->post('add_city'))
		{
			$this->data['city']=trim($this->input->post('city'));
			
			//search if this exists
			if(strcmp($this->data['province_list'][$this->input->post('province_list')], "")!=0 && strcmp($this->data['city'],"")!=0)
			{
				$result=$this->Admin_Input_Model->search_city($this->data['city'], $this->data['province_list'][$this->input->post('province_list')]);
				
				if($result==1)
				{
					$data['warning']="City under this province/State already exists.";
				}
				else
				{
					//add this city under the province
					//add this city under the province
					if($this->Admin_Input_Model->add_city($this->data['city'], $this->data['province_list'][$this->input->post('province_list')]))
						$this->data['success']="City ".$this->data['city']." was added successfully to the Province/State ".$this->data['province_list'][$this->input->post('province_list')];
					else
						$this->data['warning']=$data['warning']."\nsomething went wrong while adding the city";
				}
			}
			else if(strcmp($this->data['province_list'][$this->input->post('province_list')], "")==0)
			{
				$this->data['warning']="Province not selected.";
			}
			else if(strcmp($this->data['city'],"")==0)
			{
				$this->data['warning']="City not typed in.";
			}
		}
		if($this->input->post('add_province'))
		{
			$this->data['province_state']=trim($this->input->post('province_state'));
			$this->data['country']=trim($this->input->post('country'));
			
		//	echo $this->data['province_state']." ".$this->data['country'];
			
			if(strcmp($this->data['province_state'], "")!=0 && strcmp($this->data['country'],"")!=0)
			{
				$result=$this->Admin_Input_Model->search_province_state($this->data['province_state']);

				if($result>0)
				{
					$this->data['warning']="Province already exists.";
				}
				else
				{
					if($this->Admin_Input_Model->add_province($this->data['province_state'], $this->data['country']))
						$this->data['success']="Province ".$this->data['province_state']." was added successfully to the Country ".$this->data['country'];
					else
						$this->data['warning']=$data['warning']."\nsomething went wrong while adding the city";
						
				}
			}
			else if(strcmp($this->data['province_state'],"")==0)
			{
				$this->data['warning']="Province/State name was not entered.";
			}
			else if(strcmp($this->data['country'],"")==0)
			{
				$this->data['warning']="Country name was not entered.";
			}
		}
		
		if($this->input->post('search_quote'))
		{
			$start = $this->input->post('start_date');
			$end = $this->input->post('end_date');
			
			if(strcmp($start,"")!=0 && strcmp($end,"")!=0)
			{
				$month = strtok($start, "/");
				$date = strtok("/");
				$year = strtok("/");
				
				$start = $year."-".$month."-".$date;

				echo $start;
				
				$month = strtok($start, "/");
				$date = strtok("/");
				$year = strtok("/");
				
				$end = $year."-".$month."-".$date;

				echo $start;
				
				$this->data['quote_list'] = $this->Admin_Input_Model->list_quotation($start, $end);
			}
			else
			{
				$this->data['warning']="The start and end dates need be entered.";
			}
		}
		
		if($this->input->post('quotation_detail'))
		{
			$id = $this->input->post('hidden_quote_id');

			$this->read_quotation($id);
		}
		
		if($this->input->post('search_city'))
		{
			$this->data['search']=trim($this->input->post('search'));
			
			if(strcmp($this->data['search'],"")!=0)
			{
	//			echo $this->data['search']."|";
				$this->data['search_city_list']=$this->Admin_Input_Model->list_search_city($this->data['search']);
			}
				
		}
		if($this->input->post('search_province'))
		{
			
			$this->data['search_province']=trim($this->input->post('search_province_state'));
			
			if(strcmp($this->data['search_province'],"")!=0)
			{
				$this->data['search_province_list']=$this->Admin_Input_Model->province_state_list($this->data['search_province']);
			}
		}
		if($this->input->post('delete_city'))
		{
			$this->delete_city();
			$this->load->view("admin_search_city",$this->data);
		}
		
		if($this->input->post('delete_province'))
		{
			$this->delete_province();
			$this->load->view("admin_search_prov",$this->data);
		}

		$this->load->view("admin_input",$this->data);

	}
	
	function get_province_list()
	{
		return $this->Admin_Input_Model->get_province_list();
	}
	
	function delete_city($id)
	{
/*	
		$this->data['hidden_city_id']=$this->input->post('hidden_city_id');
		
		if($this->data['hidden_city_id']>=0)
		{	
			$this->Admin_Input_Model->delete_city($this->data['hidden_city_id']);
			$this->data['success']="City deleted successfully";
		}
		else
			echo $this->data['hidden_city_id'];
*/
		if($id>=0)
		{	
			$this->Admin_Input_Model->delete_city($id);
			$this->data['success']="City deleted successfully";
		}
		else
			echo $id;

		$this->load->view("admin_search_city",$this->data);
	}
	
	function delete_province($id)
	{
	//	$this->data['hidden_province_id']=$this->input->post('hidden_province_id');
		
		if($id>=0)
		{
			$this->Admin_Input_Model->delete_province($id);
			$this->data['success']="Province/State deleted successfully";
		}
		else
			echo $id;

		$this->load->view("admin_search_prov",$this->data);
	}
	
	function read_quotation($id)
	{
		$quotation=$this->Admin_Quotation_Model->read_quotation($id);

		if($quotation)
		{
			$this->data['requester_name']=$quotation->requester_name;
	//		$this->data['requesting_comp_name']=$quotation->requesting_comp_name;
	//		$this->data['your_number']=;
			$this->data['email']=$quotation->email;
			$this->data['requester_phone']=$quotation->phone;
			$this->data['designator_id']=$quotation->designation_id;
			$this->data['designator_title']=$this->Admin_Quotation_Model->get_designation_id($this->data['designator_id']);
			$this->data['pickup_date']=$quotation->pickup_date;
			$this->data['goods_description']=$quotation->goods_description;
			$this->data['other_comment']=$quotation->other_comment;
			$this->data['shipdest_comment']=$quotation->shipping_desctination_comment;
			$this->data['hand_inst']=$quotation->handling_instruction;
			$this->data['pickup_date']=$quotation->pickup_date;
			$this->data['pickup_time']=$quotation->pickup_time;
			$this->data['dock_closed_time']=$quotation->dock_closing_time;
			$this->data['total_weight']=$quotation->total_weight;
			$this->data['is_hazardous']=$quotation->is_hazardous;
			$this->data['total_pieces']=$quotation->pieces;
			
			$address=$this->Admin_Quotation_Model->read_address($quotation->origin_address_id);
			$city=$this->Admin_Quotation_Model->get_city($address->city_id);
			$this->data['pickup_city']=$city->city_name;

			$province=$this->Admin_Quotation_Model->get_province($address->province_id);
			$this->data['pickup_province']=$province->state_province_name;
			$this->data['pickup_contact_name']=$address->name;
			$this->data['pickup_country']=$province->country_name;
			$this->data['pickup_address']=$address->street;
			$this->data['pickup_p_code']=$address->postal_code;
			$this->data['pickup_fax']=$address->fax;
			$this->data['pickup_company_name']=$address->company_name;
			$this->data['pickup_phone_number']=$address->phone;

			$address=$this->Admin_Quotation_Model->read_address($quotation->destination_address_id);
			$city=$this->Admin_Quotation_Model->get_city($address->city_id);
			$this->data['destination_city']=$city->city_name;

			$province=$this->Admin_Quotation_Model->get_province($address->province_id);
			$this->data['destination_country']=$province->country_name;
			$this->data['destination_province']=$province->state_province_name;
			$this->data['destination_contact_name']=$address->name;;
			$this->data['destination_address']=$address->street;
			$this->data['destination_p_code']=$address->postal_code;
			$this->data['destination_fax']=$address->fax;
			$this->data['destination_company_name']=$address->company_name;
			$this->data['destination_phone_number']=$address->phone;
		}
		$this->load->view("admin_quotation",$this->data);
	}

	
}
?>